package com.cham.video.controller;

import com.cham.enums.ChamSysCodeEnum;
import com.cham.video.dto.VideoInfo;
import com.cham.video.service.RedisService;
import com.cham.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 请描述一下这个类文件吧
 * @createTime 2020年03月15日 09:37:00
 */
@Controller
@RequestMapping("/videoController")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private RedisService redisService;




    @GetMapping("/getVideoInfoByFenzuId")
    @ResponseBody
    public List<VideoInfo> getVideoInfoByFenzuId(int fenzuId){
        return videoService.getVideoInfoByFenzuId(fenzuId);
    }



    @GetMapping("/getVideoInfoRandom10ByFenquParent")
    @ResponseBody
    public List<VideoInfo> getVideoInfoRandom10ByFenquParent(String fenquParent){
        return videoService.getVideoInfoRandom10ByFenquParent(fenquParent);
    }

    @GetMapping("/getXinfan1dayTop10")
    @ResponseBody
    public List<VideoInfo> getXinfan1dayTop10(){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("xinfan-info-1day-top10-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getXinfan1dayTop10();
            redisService.saveVideoInfoListCache("xinfan-info-1day-top10-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getXinfan3dayTop10")
    @ResponseBody
    public List<VideoInfo> getXinfan3dayTop10(){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("xinfan-info-3day-top10-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getXinfan3dayTop10();
            redisService.saveVideoInfoListCache("xinfan-info-3day-top10-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getXinfan7dayTop10")
    @ResponseBody
    public List<VideoInfo> getXinfan7dayTop10(){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("xinfan-info-7day-top10-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getXinfan7dayTop10();
            redisService.saveVideoInfoListCache("xinfan-info-7day-top10-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getVideoInfo1dayTop10ByFenquParent")
    @ResponseBody
    public List<VideoInfo> getVideoInfo1dayTop10ByFenquParent(String fenquParent){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("video-info-1day-top10-"+fenquParent+"-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getVideoInfo1dayTop10ByFenquParent(fenquParent);
            redisService.saveVideoInfoListCache("video-info-1day-top10-"+fenquParent+"-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getVideoInfo3dayTop10ByFenquParent")
    @ResponseBody
    public List<VideoInfo> getVideoInfo3dayTop10ByFenquParent(String fenquParent){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("video-info-3day-top10-"+fenquParent+"-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getVideoInfo3dayTop10ByFenquParent(fenquParent);
            redisService.saveVideoInfoListCache("video-info-3day-top10-"+fenquParent+"-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getVideoInfo7dayTop10ByFenquParent")
    @ResponseBody
    public List<VideoInfo> getVideoInfo7dayTop10ByFenquParent(String fenquParent){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("video-info-7day-top10-"+fenquParent+"-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getVideoInfo7dayTop10ByFenquParent(fenquParent);
            redisService.saveVideoInfoListCache("video-info-3day-top10-"+fenquParent+"-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getVideoInfo1dayTop10ByFenquParentAndChild")
    @ResponseBody
    public List<VideoInfo> getVideoInfo1dayTop10ByFenquParentAndChild(String fenquParent,String fenquChild){
        return videoService.getVideoInfo1dayTop10ByFenquParentAndChild(fenquParent,fenquChild);
    }

    @GetMapping("/getVideoInfo3dayTop10ByFenquParentAndChild")
    @ResponseBody
    public List<VideoInfo> getVideoInfo3dayTop10ByFenquParentAndChild(String fenquParent,String fenquChild){
        return videoService.getVideoInfo3dayTop10ByFenquParentAndChild(fenquParent,fenquChild);
    }

    @GetMapping("/getVideoInfo7dayTop10ByFenquParentAndChild")
    @ResponseBody
    public List<VideoInfo> getVideoInfo7dayTop10ByFenquParentAndChild(String fenquParent,String fenquChild){
        return videoService.getVideoInfo7dayTop10ByFenquParentAndChild(fenquParent,fenquChild);
    }

    @GetMapping("/getCountOfVideoByFenquParentAndChild")
    @ResponseBody
    public int getCountOfVideoByFenquParentAndChild(String fenquParent,String fenquChild){
        return videoService.getCountOfVideoByFenquParentAndChild(fenquParent,fenquChild);
    }


    /**
     * 從大分區裏找出排行前9的視頻
     * @param fenquParent
     */

    @GetMapping("/getVideoInfoTop12ByFenquParent")
    @ResponseBody
    public List<VideoInfo> getVideoInfoTop12ByFenquParent(String fenquParent){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("video-info-top12-"+fenquParent+"-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getVideoInfoTop12ByFenquParent(fenquParent);
            redisService.saveVideoInfoListCache("video-info-top12-"+fenquParent+"-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getVideoInfoTop12ByFenquParentAndChild")
    @ResponseBody
    public List<VideoInfo> getVideoInfoTop12ByFenquParentAndChild(String fenquParent, String fenquChild){
        return  videoService.getVideoInfoTop12ByFenquParentAndChild(fenquParent,fenquChild);
    }

    @GetMapping("/getVideoInfoFenyeByFenquParentAndChild")
    @ResponseBody
    public List<VideoInfo> getVideoInfoFenyeByFenquParentAndChild(String fenquParent, String fenquChild, int start, int pageSize){
        return  videoService.getVideoInfoFenyeByFenquParentAndChild(fenquParent,fenquChild,start,pageSize);
    }




    @GetMapping("/getXinfanCurrentUpdate")
    @ResponseBody
    public List<VideoInfo> getXinfanCurrentUpdate(){
        return videoService.getXinfanCurrentUpdate();
    }

    @GetMapping("/getJingxuanTop4Video")
    @ResponseBody
    public List<VideoInfo> getJingxuanTop4Video(String fenquParent){
        return videoService.getJingxuanTop4Video(fenquParent);
    }




//    @GetMapping("/getXinfanTop12")
//    @ResponseBody
//    public List<VideoInfo> getXinfanTop12(){
//        List<VideoInfo> videoInfoList = null;
//        videoInfoList = redisService.getVideoInfoListCache("xinfan-info-top12-cache");
//        if(videoInfoList==null || videoInfoList.size()==0){
//            videoInfoList = videoService.getXinfanTop12();
//            redisService.saveVideoInfoListCache("xinfan-info-top12-cache",videoInfoList);
//        }
//        return videoInfoList;
//    }



//    @GetMapping("/getVideoInfoTop10TodayByFenquParent")
//    @ResponseBody
//    public List<VideoInfo> getVideoInfoTop10TodayByFenquParent(String fenquParent){
//        List<VideoInfo> videoInfoList = null;
//        videoInfoList = redisService.getVideoInfoListCache("video-info-top10-today-"+fenquParent+"-cache");
//        if(videoInfoList==null || videoInfoList.size()==0){
//            videoInfoList = videoService.getVideoInfoTop10TodayByFenquParent(fenquParent);
//            redisService.saveVideoInfoListCache("video-info-top10-today-"+fenquParent+"-cache",videoInfoList);
//        }
//        return videoInfoList;
//    }



//    @GetMapping("/getVideoInfoFenquParentTop11")
//    @ResponseBody
//    public List<VideoInfo> getVideoInfoFenquParentTop11(String fenquParent){
//        List<VideoInfo> videoInfoList = null;
////        videoInfoList = redisService.getVideoInfoListCache(fenquParent+"top11Cache");
////
////
////        if(videoInfoList==null || videoInfoList.size()==0){
////            videoInfoList = videoService.getVideoInfoFenquParentTop11(fenquParent);
////            redisService.saveVideoInfoListCache(fenquParent+"top11Cache",videoInfoList);
////        }
//        videoInfoList = videoService.getVideoInfoFenquParentTop11(fenquParent);
//        return videoInfoList;
//    }

    @GetMapping("/getVideoInfoSpecial")
    @ResponseBody
    public List<VideoInfo> getVideoInfoSpecial(){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("video-info-special-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getVideoInfoSpecial();
            redisService.saveVideoInfoListCache("video-info-special-cache",videoInfoList);
        }
        return videoInfoList;
    }

    @GetMapping("/getVideoInfoTop6")
    @ResponseBody
    public List<VideoInfo> getVideoInfoTop6(){
        List<VideoInfo> videoInfoList = null;
        videoInfoList = redisService.getVideoInfoListCache("video-info-top6-cache");
        if(videoInfoList==null || videoInfoList.size()==0){
            videoInfoList = videoService.getVideoInfoTop6();
            redisService.saveVideoInfoListCache("video-info-top6-cache",videoInfoList);
        }
        return videoInfoList;
//        return videoService.getVideoInfoTop5To10Today();
    }

    //为某个视频新增一次播放次数
    @PostMapping("/addBofangCount")
    @ResponseBody
    @Transactional
    public void addBofangCount(@RequestParam("videoId") int videoId) {
        videoService.addBofangCount(videoId);
    }


}
